/*
 * main.c
 *
 *  Created on: Nov 25, 2012
 *      Author: andrey
 */

#include <stm32f4xx_gpio.h>
#include <stm32f4xx_rcc.h>
#include <misc.h>

#include "my_lib.h"
#include "rficonsole.h"
#include "stm324xg_eval.h"
#include "lcd_SSD1289.h"
#include "drawing.h"
#include "my_timer.h"
#include "rficonsole_stm32.h"
#include "data_buffer.h"

#include "denso_adc_logic.h"

#define GPIOD_LED4              12      /* Green LED.   */
#define GPIOD_LED3              13      /* Orange LED.  */
#define GPIOD_LED5              14      /* Red LED.    1 */
#define GPIOD_LED6              15      /* Blue LED.    */

#define PAL_PORT_BIT(n) ((1 << (n)))

extern data_buffer_s IC1;
extern data_buffer_s IC2;
extern data_buffer_s times;
extern data_buffer_s counts;
extern data_buffer_s values;

int main(void) {

	SysTick_Config(SystemCoreClock / 1000); // 1 ms in each tick

	mySTM_EVAL_LEDInit(PAL_PORT_BIT(GPIOD_LED3), GPIOD );
	mySTM_EVAL_LEDInit(PAL_PORT_BIT(GPIOD_LED4), GPIOD );

	turnPinOn(GPIOD, PAL_PORT_BIT(GPIOD_LED3)); /* Orange.  */
	turnPinOn(GPIOD, PAL_PORT_BIT(GPIOD_LED4)); /* Blue.  */

#ifdef USE_LCD
	LCD_SSD1289_Init();
	LCD_Clear(LCD_COLOR_BLUE);

	LCD_SetFont(&Font12x12);
	LCD_SetTextColor(LCD_COLOR_WHITE);
	LCD_SetBackColor(LCD_COLOR_BLUE2);
	LCD_DisplayStringLine(0, 0, VERSION_STRING);
	LCD_DisplayStringLine(0, 12, "PWM6");
#endif

	initConsole();
	initAdcDriver();




//	initAdcUsart();

	char buffer[] = "              ";

	int prevCounter = -1;
	int loopCoutner = 0;
	int prevDbSize = -1;
	int prevV = -1;

	//adcUsartPoke();

	Delay(150);
	requestAdcReading(15);
	Delay(150);

	while (1) {
		loopCoutner++;
		Delay(50);
		turnPinOn(GPIOD, PAL_PORT_BIT(GPIOD_LED4)); /* Blue.  */
		Delay(50);
		turnPinOff(GPIOD, PAL_PORT_BIT(GPIOD_LED4)); /* Green.  */

//		if (IC1.size != prevDbSize && (IC1.size % 8 == 0)) {
//			prevDbSize = IC1.size;
//			printDataBuffer(&IC1, "ic1");
//			printDataBuffer(&IC2, "ic2");
//			printDataBuffer(&times, "times");
//			printDataBuffer(&counts, "counts");
//			printDataBuffer(&values, "values");
//		}

//		if (loopCoutner % 5 == 0) {
//			int adc = adcGetvalue();
//			int adc2 = adcGetvalue2();
//
//			int IC1Value = getIC1Value();
//			int IC2Value = getIC2Value();
//
////			print("IC1Value %d %d\r\n", IC1Value, IC2Value);
//
//			if (adc != 20) {
////				print("adc =%d\r\n", adc);
////				print("adc2=%d\r\n", adc2);
//			}
//		}

		int v = readAdcValue();
		if (v != prevV) {
			prevV = v;
			print("value=%d\r\n", v);
		}

		itoa(0, buffer);
#ifdef USE_LCD
		LCD_DisplayStringLine(0, 36, buffer);
#endif

		int cou = getEventCounter();
		if (cou != prevCounter) {
			prevCounter = cou;
			itoa(cou, buffer);
#ifdef USE_LCD
			LCD_DisplayStringLine(0, 24, buffer);
#endif
//			print("counter %d\r\n", cou);
		}

		if (hasStmConsoleLine()) {
			print("Still alive [%s]\r\n", getStmConsoleLine());
			handleConsoleLine(getStmConsoleLine());
			clearConsoleLine();
		}

	}
	return 0;
}
